package com.waiting.nio;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Description 利用FileChannel实现文件的读取功能
 * @Author bjweitingting
 * @Date 2018/10/19
 **/
public class NioFileTest {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\bjweitingting/Desktop/test.txt";
        readFileByFileChannel(filePath);
    }

    public static void readFileByFileChannel(String filePath) {
        try (RandomAccessFile accessFile = new RandomAccessFile(filePath, "rw")) {
            FileChannel fileChannel = accessFile.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(48);
            int count = fileChannel.read(byteBuffer);
            while (count > -1) {
                System.out.println(byteBuffer);
                byteBuffer.flip();
                count = fileChannel.read(byteBuffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
